$(function () {
    /**
     * 登入
     */
    $('#submitLogin').click(function () {
        $.ajax({
            url: '/api/passport/login',
            type: 'post',
            dataType: 'json',
            data: { 'email': $('#email').val(), 'password': $('#password').val() },

            beforeSend: function () {//提交数据前执行判断，根据返回t/f决定是否发送
                return true
            },

            success: function (result, status) {

                if (result.ec == '200') {
                    //成功
                    mdui.snackbar({
                        message: '登录成功!正在跳转',
                        position: 'left-top'
                    });
                    //附加功能
                    $.cookie('UUID', result.data.UUID, { expires: 7, path: '/' });
                    window.location.replace('/v/user');
                    return;
                } else {
                    //失败
                    var arrData = result.data;
                    var reuData = '';
                    for (let index in arrData) {
                        reuData = reuData + arrData[index] + '&nbsp;';
                    }
                    //详细输出
                    reuData = '<br>' + result.ec + '&nbsp;:&nbsp;' + reuData;
                    mdui.snackbar({
                        message: 'msg&nbsp;:&nbsp;' + result.msg + reuData,
                        position: 'left-top'
                    });
                    return;
                }
            },

            error: function () {
                mdui.snackbar({
                    message: '4XX&nbsp;:&nbsp;未知错误，请稍后重试',
                    position: 'left-top'
                });
            }
        })
    })

    /**
     * 注册
     */
    $('#submitRegister').click(function () {
        $.ajax({
            url: '/api/passport/register',
            type: 'post',
            dataType: 'json',
            data: { 'email': $('#email').val(), 'username': $('#username').val(), 'password': $('#password').val(), 'rpassword': $('#rpassword').val() },

            beforeSend: function () {//提交数据前执行判断，根据返回t/f决定是否发送
                return true
            },

            success: function (result, status) {

                if (result.ec == '200') {
                    //成功
                    mdui.snackbar({
                        message: '注册成功!正在跳转',
                        position: 'left-top'
                    });
                    //附加功能
                    $.cookie('UUID', result.data.UUID, { expires: 7, path: '/' });
                    window.location.replace('/v/user');
                    return;
                } else {
                    //失败
                    var arrData = result.data;
                    var reuData = '';
                    for (let index in arrData) {
                        reuData = reuData + arrData[index] + '&nbsp;';
                    }
                    //详细输出
                    reuData = '<br>' + result.ec + '&nbsp;:&nbsp;' + reuData;
                    mdui.snackbar({
                        message: 'msg&nbsp;:&nbsp;' + result.msg + reuData,
                        position: 'left-top'
                    });
                    return;
                }
            },

            error: function () {
                mdui.snackbar({
                    message: '4XX&nbsp;:&nbsp;未知错误，请稍后重试',
                    position: 'left-top'
                });
            }
        })
    })

});

